<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @abstract 公共控制器
 * @author zxj
 * @description 存放公共访问的处理器，常用与AJAX请求
 */
class Common extends CI_Controller {
    function __construct() {
        parent::__construct();
        //加载模型
        $this->load->model('order_model');
        $this->load->model('product_model');
        //分析器
        $this->output->enable_profiler(TRUE);
    }



    function testSoap()
    {
        //var_dump ($result);
        $client = new SoapClient('http://tmp.tomrepair.es/api/soap/?wsdl');
        $session = $client->login('Pandawilljason', 'pandawillJason@#23&Pdw^jor23dan*');
        $data = array('10', '1', '0');
        $result = $client->call($session, 'weixiuzhan_activity.getListOrder', $data);
        var_dump($result);
    }




    function status(){
        $sql = 'select * from maintain_status';
        $statusList = $this->common_model->getQuery($sql,'result_array');
        $newList = array();
        foreach ($statusList as $rs) {
            $newList[$rs['status_name_cn']] = $rs['status_name_en'];
        }
        $newList['待报价'] = 'Waiting for quote';
        $newList['拒绝报价'] = 'Disagree';
        $newList['质检'] = 'QC';
        gsh_output($newList);
        $allList = $this->common_model->selectAll('mt_product_status');
        foreach ($allList as $rs) {
            if($newList[$rs['name_ch']]){
                $updata = array('name_en'=>$newList[$rs['name_ch']]);
                $this->common_model->update('mt_product_status',$updata,array('id'=>$rs['id']));
            }
        }

    }



    function sendEmail(){
        echo '邮件发送中....';
        $sendResult = sendEmail(array('assign'=>'email_payPrice','emails'=>'921472314@qq.com','code'=>'mt_rand()'));
        var_dump($sendResult);
    }



    function accept_price(){
        $str = file_get_contents('http://www.baidu.com/sc/products/product/accept_price/9780/1');
        echo $str;
    }


    function daochu_track(){
        $sql = 'select o.maintain_id,o.status_id,o.source_id,o.source_int_id,o.consumer_email,o.consumer_name,o.maintain_type_id,o.repairer,t.* from maintain_order as o
        left join maintain_track as t on o.maintain_id=t.maintain_id where t.target_status=23 and (t.add_time>=\'2016-01-01\' and t.add_time<=\'2016-02-01\') ';
        $dataList = $this->common_model->getQuery($sql,'result_array');
        $fieldData = array(
            'maintain_id'=>'维修单号','consumer_email'=>'客户邮箱',
            'target_status'=>'处理状态','modified_user'=>'处理人','add_time'=>'处理时间'
        );
        excel_output('维修数据',$fieldData,$dataList);
    }


    function daochu_smtwlm(){
        $sql =
            '
        select o.maintain_id,o.source_id,o.consumer_name,o.consumer_phone,o.consumer_email,o.add_time,
p.maintain_id as oid,p.is_guarantee from maintain_order as o
left join maintain_product as p  on o.maintain_id = p.maintain_id
where o.website_origin=6 or o.source_site_id=9

        ';

        $dataList = $this->common_model->getQuery($sql,'result_array');
        foreach ($dataList as &$rs) {
            $sql = '
            select t.*,s.* from maintain_track as t
left join maintain_status as s on t.target_status=s.status_id
where t.maintain_id = \''.$rs['oid'].'\' order by t.track_id desc;
            ';
            $statusFrist = $this->common_model->getQuery($sql,'row_array');
            $rs['status_name'] = $statusFrist?$statusFrist['status_name_cn']:'待接收';
        }
        unset($rs);

        $fieldData = array(
            'maintain_id'=>'维修单号','consumer_email'=>'客户邮箱','consumer_name'=>'客户姓名',
            'consumer_phone'=>'客户电话','status_name'=>'当前状态',
            'add_time'=>'创建时间'
        );
        //gsh_output($dataList,1);
        excel_output('aliexpress',$fieldData,$dataList);
    }



    function daochu_zt(){
        $sql = 'select a.maintain_id,b.* from maintain_order a left join maintain_product b  on a.maintain_id=b.maintain_id  where a.add_time>=\'2015-11-01\' and a.add_time<=\'2016-01-31\' and b.is_guarantee=2 and b.brand_id=11 ORDER BY a.add_time';
        $datalist = $this->common_model->getQuery($sql,'result_array');
        foreach ($datalist as $key=>&$rs) {
            $priceData = $this->common_model->selectFirst('maintain_product_price',array('pid'=>$rs['product_id']));
            $rs['part'] = $priceData['part'];
            $rs['maintain_price'] = $rs['maintain_price'];
            //获取配件
            $peijianList = $this->common_model->selectCustom('maintain_accessory',array('pid'=>$rs['product_id']));
            foreach ($peijianList as $pj) {
                $rs['product_sku'] .= '  '. $pj['sku'];
                $rs['product_sku_name'] .=  '  '.$pj['accessory_name'];
                $rs['peijian_price'] .= '   '.$pj['accessory_price'];
                $rs['peijian_cost'] .= '   '.$pj['accessory_cost'];
            }

            //获取配件价格
            $rs['price_total_sgf'] += $priceData['maintain_price'];
            $rs['price_total_yf'] += $priceData['freight_price'];
            $rs['price_total_pjf'] += $priceData['accessory_price'];
            $rs['cp_price_total'] += $priceData['maintain_price']+$priceData['freight_price']+$priceData['accessory_price'];
            $rs['price_sort'] = $priceData['price_sort'];
            $rs['product_quantity'] = max($rs['product_quantity'],1);
        }
        unset($rs);
        $fieldData = array(
            'maintain_id' => '维修单号','part'=>'维修项','imei'=>'imei','product_sku_name'=>'配件',
            'price_total_sgf'=>'手工费','sku'=>'SKU','add_time'=>'创建日期','product_sku'=>'物料SKU'
        );
        excel_output('数据列表', $fieldData, $datalist);
    }



    function daochu_cw(){
        $strIds = array(

            'de00002131','de00002162','de00002182','es00006829','es00007284','es00007292','es00004446','es00004226','es00005222','es00007064','es00007480','es00007390','es00007512','es00006902','es00007628','de00002166','es00007246','de00002147','es00006976','es00007724','es00007112','es00007400','es00007590','es00007106','es00007728','es00007808','es00007014','es00007721','es00007786','es00007241','es00008005','es00007377','de00002263','de00002243','es00007574','es00007692','es00007709','es00007767','es00007773','es00007773','es00007231','es00007826','es00007170','es00007264','es00007976','es00007304','es00007445','es00007132','es00007778','es00007772','es00007854','es00007552','de00002289','de00002291','es00006860','es00004828','es00004896','es00006473','es00006846','es00007456','es00007448','es00006594','es00006819','es00006969','es00007316','es00007419','es00008112','es00008145','es00008198','es00007530','es00007762','es00007351','es00007719','de00002352','es00007020','es00008064','de00002276','es00007423','de00002343','de00002333','es00008383','es00007629','de00002361','de00002295','de00002318','de00001672','es00004252','es00006247','es00005365','de00002354','de00002329','es00006760','es00007793','es00005475','es00005475','de00002159','es00006779','es00007884','es00007827','es00006313','es00007950','es00007136','es00008019','es00007691','es00008118','es00007483','de00002483','de00002380','de00002462','es00007436','es00008496','es00008065','es00007949','es00006451','es00007442','es00006963','es00007260','es00007375','es00007578','es00007468','es00007426','es00007322','es00007951','es00007872','es00007667','es00007371','es00007441','es00007529','es00007230','es00007648','es00007695','es00007495','es00007632','es00007549','es00007975','es00007633','es00006560','es00007131','es00007680','es00007346','de00002425','es00007794','es00007446','es00007660','es00007405','es00007630','es00007527','es00007033','es00007122','es00007300','es00007715','es00007551','es00007740','es00007508','es00007403','es00007368','es00007647','es00007713','es00007697','es00007372','es00007138','es00007694','es00007823','es00007420','es00007765','es00008336','es00007610','es00008050','es00007690','es00007970','es00007820','es00008214','es00007712','es00007544','es00007821','es00007754','es00007802','es00007822','es00007693','es00008072','es00008120','es00008027','es00008073','es00008136','es00007602','es00008040','es00007817','es00007873','es00008089','es00008063','es00008046','es00005479','es00008058','es00008277','es00008163','es00008165','es00008060','es00008219','es00008161','es00008250','es00006552','es00006664','es00008280','es00008103','es00007408','es00008062','es00007964','es00004909','es00008216','es00008332','es00007656','es00008238','es00008364','es00007472','es00007517','es00008099','es00007516','es00008101','es00008133','es00007824','es00007245','es00007617','es00007813','es00007726','es00007505','es00007096','es00008056','es00008183','es00007125','es00007212','es00007354','es00007885','es00007732','es00007282','es00007665','es00007437','es00007482','es00007369','es00008189','es00007597','es00007766','es00007573','es00007684','es00007608','es00007882','es00008246','es00008207','es00007722','es00008300','es00008232','es00007971','es00008290','es00007982','es00008206','es00008162','es00006197','es00005362','es00008102','es00007152','es00008057','es00007538','es00007511','es00007686','es00007514','es00007711','es00008259','es00008320','es00008119','es00008224','es00008231','de00002391','de00002384','de00002373','de00002428','de00002386','de00002378','de00002304','de00002438','es00007977','es00008016','es00008114','es00008283','es00008066','es00008269','es00008399','es00008221','es00008248','es00008225','es00008297','es00007993','es00008374','es00008244','es00008265','es00007688','de00002436','es00008160','de00002432','es00008118','de00002418','de00002443','de00002388','de00002421','es00008303','es00004825','es00008282','es00008305','es00003348','es00008349','es00007537','es00006272','es00008440','es00008429','es00008354','es00008442','de00002551','de00002407','de00002445','de00002488','es00005312','de00002509','de00002500','de00002423','de00002452','de00002413','de00002540','de00002501','de00002503','de00002433','de00002480','de00002424','de00002377','de00002486','es00007152','es00006849'

        );

        foreach ($strIds as $val) {
            $val = strtolower(trim($val));
            if(!$val)$val='empty';
            /*
            $addWhere = preg_match('/^15000\d{4}$/',$val)?' o.repair_site_id=1 and ':'';
            $addWhere = preg_match('/^es\d+$/',$val)?' o.repair_site_id=1 and ':'';
            */
            /*
            if($addWhere || preg_match('/^[\d]{4}\/[\d]+$/',$val)){
                $addWhere .= " (source_int_id = '$val' or  maintain_id = '$val' or source_track_num like '%$val%' or track_num like '%$val%')";
            }else{
                $addWhere = preg_match('/^[^\d]*$/',$val)?' o.consumer_name like \'%'.$val.'%\' ':'';
                if(!$addWhere){
                    $addWhere = ' o.consumer_address like \'%'.$val.'%\' ';
                }
            }
            */
            $addWhere = " maintain_id = '$val'";
            $sql = "select o.maintain_id,o.consumer_email,o.repair_site_id,o.maintain_type_id,o.source_int_id,c.* from maintain_order as o
left join mt_customers as c ON o.consumer_email=c.email
where $addWhere ;";
            $orderFirst = $this->common_model->getQuery($sql,'row_array');
            if($orderFirst && !$orderFirst['name'])
            {
                $orderFirst['name']='未匹配出部门';
            }
            $orderFirst['name'] = str_replace('DHHT_','',$orderFirst['name']);
            echo $orderFirst?$orderFirst['name']:'未查到此数据';
            echo '<br/>';
        }

        /*
        $sql = "select * from maintain_order where repair_site_id=1  and maintain_type_id=6 and source_int_id in('150000152','150000189','150000224','150000254','150000258','150000274','150000274','150000293','150000329','150000329','150000329','150000336','150000340','150000409','150000420','150000504','150000509','150000585','150000587','150000611','150000642','150000655','150000662','150000665','150000677','150000681','150000706','150000706','150000720','150000738','150000762','150000766','150000769','150000779','150000799','150000811','150000833','150000880','150000889','150000889','150000922','150000926','150000927','150000929','150000934','150000934','150000950','150000964','150000968','150000973','150000980','150000997','150001007','150001010','150001029','150001034','150001036','150001036','150001059','150001063','150001091','150001110','150001112','150001132','150001132','150001133','150001143','150001144','150001200','150001203','150001207','150001208','150001210','150001215','150001220','150001227','150001236','150001241','150001269','150001273','150001276','150001280','150001291','150001292','150001297','150001313','150001314','150001319','150001321','150001323','150001327','150001330','150001340','150001351','150001377','150001377','150001385','150001387','150001390','150001405','150001460','150001475','150001495','150001529','150001565','150001670','150001695','150001699','150001704','150001705','150001706','150001710','150001716','150001735','150001782','150001785','150000705','150000933');";
        $datalist = $this->common_model->getQuery($sql,'result_array');
        foreach ($datalist as $key=>&$rs) {
            $shopFrist = $this->common_model->selectFirst('mt_customers',array('email'=>$rs['consumer_email']));
            $rs['email_name'] = $shopFrist['name'];
            $rs['email'] = $shopFrist['email'];
        }
        unset($rs);
        $fieldData = array(
            'source_int_id'=>'单号',
            'maintain_id' => '维修单号','email_name'=>'部门','email'=>'email'
        );
        if(gp('debug')){
            gsh_output($datalist);
        }
        excel_output('数据列表', $fieldData, $datalist);
        */

    }



    function daochu_cg($stime='2016-01-01',$etime='2016-12-30'){

        $sql = "SELECT o.maintain_id,o.maintain_type_id,o.consumer_email,b.*,t.* FROM maintain_order as o
left join mt_customers as b on o.consumer_email = b.email
left join maintain_track as t on o.maintain_id = t.maintain_id
WHERE maintain_type_id =  '8' AND t.target_status=2 AND t.add_time > '$stime' AND t.add_time < '$etime'  ";
        $orderList = $this->common_model->getQuery($sql,'result_array');
        foreach ($orderList as &$rs) {
            $productList = $this->common_model->selectCustom('maintain_product',array('maintain_id'=>$rs['maintain_id']));
            if(!$productList)continue;
            $rs['skuStr'] = $rs['quantity'] = $rs['weixiudan_order_id'] = $rs['damage_name'] ='';
            foreach ($productList as &$pl) {
                $rs['skuStr'] .= $pl['sku'].'|';
                $rs['quantity'] += $pl['product_quantity'];
                $rs['weixiudan_order_id'] .= $pl['weixiudan_order_id'].'|';
                $damageFirst = $this->common_model->selectFirst('maintain_damage',array('damage_id'=>$pl['damage_id']));
                $rs['damage_name'] .= $damageFirst['damage_name'].'|';
            }
            $rs['skuStr'] = rtrim($rs['skuStr'],'|');
            $rs['damage_name'] = rtrim($rs['damage_name'],'|');
            $rs['weixiudan_order_id'] = rtrim($rs['weixiudan_order_id'],'|');
        }
        unset($rs);
        $fieldData = array(
            'maintain_id' => '维修单号','name'=>'部门','email'=>'email',
            'weixiudan_order_id'=>'退货单号','damage_name'=>'成色',
            'skuStr'=>'skuString','quantity'=>'数量','add_time'=>'日期'
        );
        if(gp('debug')){
            gsh_output($orderList);
        }
        excel_output('数据列表', $fieldData, $orderList);
    }



    function daochu_cw2()
    {
        $strName = "notFound;notFound;notFound;JOSE ABEL SANCHEZ;FERNANDO LOPEZ;DIANA HERNANDEZ;IE3;AINA MARSAL;IMPORTPLANET;PEDRO LALUEZA;LUIS ÓSCAR VALIÑA;JUAN LOPEZ ALONSO;ALHAURIN DE LA TO RAQUEL ROBLES ORTIZ;MOLINA DE SEGURA JOSE GALLEVO;JOSE ANTONIO SERRANO;ADRIAN DOS SANTOS GLEZ;FELIX VIÑUELA;RAFAEL ESTOP REMACHA;GABRIEL FERNANDES;DANIEL MORENPO;JOAO TIAGO PIRES;PEDRO PEREIRA;FACULDADE MEDICINA;FILIPE DIAS;RICARDO GONÇALVES;HELDER CAÇOILA;FERNANDO DOMINGOS;PAULO CORDEIRO;HELMO SILVA;MARIA LUISA;RAQUEL MAIA;DAVID CARVALHO;ORIZANDO DALVA;FABIOAZENHA;DAVID NOGUEIRA;CARLOS ANTONIO PINTO;JOANA  GOMES";
        //gsh_output(explode(';', $strName));
        foreach (explode(';', $strName) as $key=>$val) {
            $aryName = explode(' ', $val);
            $arrySize = count($aryName);
            foreach ($aryName as $k => $name) {
                $name = addslashes($name);
                $sql = "
                      select o.maintain_id,o.consumer_email,o.repair_site_id,o.maintain_type_id,o.source_int_id,c.* from maintain_order as o
                      left join mt_customers as c ON o.consumer_email=c.email where o.consumer_name like '%$name%';
                    ";
                $orderFirst = $this->common_model->getQuery($sql, 'row_array');
                if ($orderFirst) {
                    //gsh_output($key);
                    echo $orderFirst['consumer_email'] . '|' . $orderFirst['name'] . '<br />';
                    break;
                } elseif ($arrySize == ($k + 1)) {
                    //gsh_output($key);
                    echo 'notFound'.'<br />';
                }
            }
        }

    }





    /**
     * @param string $site_prefix  站点
     * @param string $start_time   开始时间
     * @param string $end_time     结束时间
     */
    function daochu_cpdata($site_prefix = 'es',$start_time = '2015-11-01',$end_time = '2015-11-30'){
        $sql = 'SELECT p.maintain_id,p.product_id,p.sku,p.imei,p.add_time,q.*
        FROM maintain_product as p LEFT JOIN maintain_qcreport as q ON p.product_id = q.pid
        WHERE p.maintain_id like \''.$site_prefix.'%\' AND p.add_time > \''.$start_time.'\' AND
        p.add_time < \''.$end_time.'\' ORDER BY p.add_time desc';
        $datalist = $this->common_model->getQuery($sql,'result_array');
        $fieldData = array(
            'maintain_id' => '维修单号', 'sku' => 'SKU','brand_name'=>'品牌名称',
            'product_model' => '品牌型号', 'fault'=>'故障','part'=>'维修项'
        );
        excel_output('数据列表', $fieldData, $datalist);
    }



    /**
     * 清理无效报价单
     */
    function clear_price(){
        $this->common_model->delete('',array('track_id'=>'20013'));
        /*
        $priceOneAll = $this->common_model->getQuery('select * from maintain_product_price','result_array');
        foreach ($priceOneAll as $rs) {
            $productFirst = $this->common_model->selectFirst('maintain_product',array('product_id'=>$rs['pid']));
            if(!$productFirst){
                $this->common_model->delete('maintain_product_price',array('pid'=>$rs['product_id']));
            }
        }
        */
    }




    function caonima(){
        $names = array('故障检测','激活/换电池（非内置的）','软件升级/刷机','重新装配','清洗','更换按键膜','更换后壳螺丝','更换翻盖螺丝','更换螺丝盖','更换转轴','更换转轴弹簧','更换按键钮(字粒)','更换前掩盖','更换后掩盖','更换前壳','更换后壳','更换电池扣','更换大镜面','更换小镜面','更换天线座','更换天线','更换耳机塞','更换射频塞','更换射频同轴线','IMEI丢失','解锁','更换主板','更换电池触点','更换SIM卡座','更换尾插','更换内置电池','更换天线触点','更换按键板','更换home 键','更换散光灯','更换排线','更换LCD/一体屏','更换前/后摄像头','更换副板','更换听筒','更换振铃（喇叭）','更换振动马达','更换麦克风 (送话器)','更换指纹感应器','更换TP','加焊','更换内侧键','更换外侧键','更换翻盖开关','更换耳机插座','更换充电USB','更换距离感应器（非主板上）','更换13MHZ晶振','更换中频IC','更换频率合成IC','更换功放IC','更换双频器','更换双工器(天线开关)','更换功率控制','更换TX   VCO','更换RX   VCO','更换滤波器','更换中频滤波器','更换32.768k (时间晶体)','更换和弦IC','更换充电IC','更换SIM卡控制 IC','更换主板内联','更换按键板内联','更换CPU','更换电源IC','更换闪存IC (字库)','更换语音编码IC (音频)','更换蓝牙模块','更换FM/收音机模块','更换照相模块','更换主板所有元器件不包括IC','更换WiFi/蓝牙IC','更换距离感应器(主板上)');
        foreach ($names as $val) {
            $laborFirst = $this->common_model->selectFirst('maintain_part',array('maintain_part_name'=>$val));
            if($laborFirst){
                echo $laborFirst['maintain_part_id'],'<br>';
            }else{
                echo $val,'<br>';
            }
        }

    }



    /**
     * 导出手工费明细
     * @param string $sitePrefix
     * @param string $start_time
     * @param string $end_time
     */
    function daochu_sgfmx($sitePrefix='es',$start_time = '',$end_time = ''){
        //获取维修站点
        $repairSite = $this->common_model->selectFirst('mt_repair_site',array('site_prefix'=>$sitePrefix));
        $siteId = $repairSite['id'];
        if(!$siteId){
            show_error('repair site is not null');
        }
        //日期规则
        $start_time = empty($start_time)?'2015-12-01':$start_time;
        $end_time = empty($end_time)?date('Y-m-d'):$end_time;
        //获取订单数据
        $sql = 'select * from maintain_order where repair_site_id=\''.$siteId.'\' and status_id>=1
        and add_time > \''.$start_time.'\' AND add_time < \''.$end_time.'\' ';
        $datalist = $this->common_model->getQuery($sql, 'result_array');
        $total_price = '';
        $repairTypeAry = array('6'=>'维修','7'=>'翻新','8'=>'退货','10'=>'返修','9'=>'递四方检查单');
        foreach ($datalist as $key=>&$rs) {
            //维修类型
            $rs['repairType'] = $repairTypeAry[$rs['maintain_type_id']];
            if($rs['source_site_id']==9){
                $rs['repairType'] = '维保单,维修类型：'.$repairTypeAry[$rs['maintain_type_id']];
            }
            //获取产品
            $rs['productList'] = $this->common_model->selectCustom('maintain_product',array('maintain_id'=>$rs['maintain_id']));
            if(!$rs['productList'])continue;
            foreach ($rs['productList'] as &$cp) {
                //if($cp['sort_id']!=1)continue;
                $priceData = $this->common_model->selectFirst('maintain_product_price',array('pid'=>$cp['product_id']));
                //gsh_output($priceData);
                $cp['priceData'] = $priceData;
                //获取配件
                $peijianList = $this->common_model->selectCustom('maintain_accessory',array('pid'=>$cp['product_id']));
                foreach ($peijianList as $pj) {
                    $rs['product_sku'] .= '  '. $pj['sku'];
                    $rs['product_sku_name'] .=  '  '.$pj['accessory_name'];
                    $rs['peijian_price'] .= '   '.$pj['accessory_price'];
                    $rs['peijian_cost'] .= '   '.$pj['accessory_cost'];
                }

                //获取配件价格
                $rs['price_total_sgf'] += $priceData['maintain_price'];
                $rs['price_total_yf'] += $priceData['freight_price'];
                $rs['price_total_pjf'] += $priceData['accessory_price'];
                $rs['cp_price_total'] += $priceData['maintain_price']+$priceData['freight_price']+$priceData['accessory_price'];
                $rs['price_sort'] = $priceData['price_sort']?$priceData['price_sort']:$repairSite['price_sort'];
                $rs['product_quantity'] = max($cp['product_quantity'],1);

                //获取成本价格
                $sql = 'select * from maintain_accessory where pid = '.$cp['product_id'];
                $materialFirst = $this->common_model->getQuery($sql, 'row_array');
                $rs['cost_total'] += $materialFirst['accessory_cost'];

                switch ($cp['is_guarantee'])
                {
                    case 0:
                        $rs['guarantee_name'] = '保外';
                        break;
                    case 1:
                        $rs['guarantee_name'] = '电商保内';
                        break;
                    case 2:
                        $rs['guarantee_name'] = '品牌保内';
                        break;
                    default:
                        $rs['guarantee_name'] = '保外';
                }
            }
            unset($cp);
            //去掉没有报价的
            if(!$rs['price_total_sgf']){
                //unset($datalist[$key]);
            }
            $total_price += $rs['cp_price_total'];
        }
        unset($rs);
        $fieldData = array(
            'maintain_id'=>'维修单号','source_int_id'=>'来源单号','repairType'=>'维修类型','consumer_email'=>'客户邮箱',
            'price_total_sgf'=>'手工费','price_total_yf'=>'运费','price_total_pjf'=>'配件费',
            'guarantee_name'=>'质保类型','price_sort'=>'货币种类','cp_price_total'=>'总额',
            'product_quantity'=>'产品数量','cost_total'=>'配件成本费','coupon_money'=>'优惠金额',
            'product_sku'=>'配件SKU','product_sku_name'=>'配件名称','peijian_price'=>'配件售价',
            'peijian_cost'=>'配件成本价',
            'add_time'=>'添加时间','repairer'=>'维修员',
        );
        $title = $sitePrefix.'_'.substr($start_time,5).'~'.substr($end_time,5);
        $sheetTitle = '总额'.$total_price.$repairSite['price_sort'];
        excel_output($title,$fieldData,$datalist,$sheetTitle);
    }


    function daochu_wxssl($name = '',$stime = '2016-01-01',$etime = '2016-02-1'){
        if(!$name){
            show_error('请输入维修师帐号');
        }
        $sql = 'select a.*,b.*,c.* from maintain_product as a
left join maintain_order as b on a.maintain_id=b.maintain_id
left join maintain_status as c on c.status_id = b.status_id
where b.repairer=\''.$name.'\' and b.add_time>=\''.$stime.'\' and b.add_time<\''.$etime.'\' ';
        $dataList = $this->common_model->getQuery($sql, 'result_array');
        $dataSize = count($dataList);
        //gsh_output($dataList,1);
        $fieldData = array(
            'maintain_id'=>'维修单号','product_id'=>'产品ID','source_int_id'=>'来源单号',
            'status_name_cn'=>'状态中文','status_name_en'=>'状态英文',
            'repairer'=>'维修员','add_time'=>'订单创建日期'
        );
        //gsh_output($productDataList,1);
        excel_output($name.$dataSize,$fieldData,$dataList);
    }


    function daochu_tuihuo(){
        $sql = "select * from maintain_order where maintain_type_id=8 AND status_id=22";
        $dataList = $this->common_model->getQuery($sql, 'result_array');
        $newList = array();
        foreach ($dataList as $rs) {
            $oid = $rs['maintain_id'];
            $where = "maintain_id = '$oid' AND (target_status = 6 or target_status = 4)";
            $trackOne = $this->common_model->selectFirst('maintain_track',$where);
            if($trackOne){
                continue;
            }else{
                $trackOne = $this->common_model->selectFirst('maintain_status',array('status_id'=>$rs['status_id']));
                $rs['trackOne'] = $trackOne;
                $rs['status_name_cn'] = $trackOne['status_name_cn'];
                //产品
                $productList = $this->common_model->selectCustom('maintain_product',array('maintain_id'=>$rs['maintain_id']));
                foreach ($productList as $row) {
                    $row['consumer_email'] = $rs['consumer_email'];
                    $row['status_name_cn'] = $rs['status_name_cn'];
                    $newList[] = $row;
                }
            }
        }
        $fieldData = array(
            'maintain_id'=>'维修单号','consumer_email'=>'邮箱','status_name_cn'=>'状态',
            'product_id'=>'产品ID','sku'=>'sku',
            'product_model'=>'型号','imei'=>'imei','add_time'=>'订单创建日期'
        );
        //gsh_output($newList);
        excel_output('tuihuo'.count($newList),$fieldData,$newList);
    }

    function websiteorigin(){
        $orders = array('es00006087','es00006067','es00005412','es00005449','es00006243','es00005495','es00006009','es00006065','es00006213','es00006089','es00006310','es00005338','es00006076','es00006137','es00005346','es00005499','es00006215','es00005341','es00006011','es00005347','es00005423','es00006211','es00005923','es00005922','es00006070','es00006173','es00006073','es00006142','es00005258','es00005459','es00006182','es00005411','es00005232','es00005838','es00005506','es00005945','es00005493','es00005833','es00005850','es00006002','es00005422','es00006072','es00006005','es00006366','es00005376','es00006085','es00005282','es00005342','es00006027','es00005783','es00006370','es00005951','es00005358','es00005950','es00005812','es00005180','es00005335','es00006242','es00005404','es00006360','es00005948','es00006231','es00006001','es00006322','es00005849','es00005466','es00006456','es00006205','es00005421','es00005425','es00005840','es00005451','es00006038','es00006003','es00006138','es00005344','es00005247','es00006406','es00006207','es00006333','es00005331','es00005940','es00006088','es00005426','es00006309','es00005505','es00005976','es00006074','es00006130','es00006214','es00005964','es00006004','es00005781','es00005952','es00005839','es00006139','es00005939','es00006010','es00006075','es00005420','es00006308','es00005492','es00005252','es00005975','es00006083','es00006180','es00005936','es00005947','es00005419','es00005410','es00006457','es00005925','es00005944','es00006040');
        foreach ($orders as $val) {
            $orderFirst = $this->common_model->selectFirst('maintain_order',array('maintain_id'=>$val));
            if($orderFirst && $orderFirst['website_origin']){
                $orginFirst = $this->common_model->selectFirst('mt_website_origin',array('id'=>$orderFirst['website_origin']));
                echo $orginFirst['name'].'<br />';
            }else{
                echo $orderFirst['consumer_email'].'<br />';
            }
        }

    }


    /**
     * 导出平衡车
     */
    function daochu_wlm($type = 'scooter'){
        if($type=='iphone'){
            $where = 'where sort_id=3 and add_time>=\'2015-12-00\' ';
        }else{
            $where = 'brand_id=73';
        }
        $sql = "select * from maintain_product $where";
        $dataList = $this->common_model->getQuery($sql, 'result_array');
        $newList = array();
        foreach ($dataList as $rs) {
            $oid = $rs['maintain_id'];
            $orderFirst = $this->common_model->selectFirst('maintain_order',array('maintain_id'=>$oid));
            $rs['consumer_email'] = $orderFirst['consumer_email'];
            $trackOne = $this->common_model->selectFirst('maintain_status',array('status_id'=>$orderFirst['status_id']));
            $rs['status_name_cn'] = $trackOne['status_name_cn'];
        }
        $fieldData = array(
            'maintain_id'=>'维修单号','consumer_email'=>'邮箱','status_name_cn'=>'状态',
            'product_id'=>'产品ID','sku'=>'sku',
            'product_model'=>'型号','imei'=>'imei','add_time'=>'订单创建日期'
        );
        //gsh_output($newList);
        excel_output('tuihuo'.count($newList),$fieldData,$newList);
    }





    /**
     * @param string $sitePrefix
     * @param string $start_time
     * @param string $end_time
     * @param int $status_id
     */
    function daochu_cpsgf2($sitePrefix='es',$start_time = '',$end_time = '',$status_id=23){
        $start_time = empty($start_time)?'2016-01-01':$start_time;
        $end_time = empty($end_time)?date('Y-m-d'):$end_time;
        $siteFirst = $this->common_model->selectFirst('mt_repair_site',array('site_prefix'=>$sitePrefix));
        $siteId = $siteFirst['id'];
        //获取订单数据
        $sql = "
        select p.*,t.*,o.* from maintain_product_price as p left join maintain_track as t on p.oid=t.maintain_id
left join maintain_order as o on p.oid=o.maintain_id
where t.target_status=$status_id and p.is_accept_price=1 and o.repair_site_id=$siteId and t.add_time>='$start_time' and t.add_time<'$end_time'
        ";
        $datalist = $this->common_model->getQuery($sql, 'result_array');
        $total_price = '';
        $productDataList = array();
        $repairTypeAry = array('6'=>'维修','7'=>'翻新','8'=>'退货','10'=>'返修','9'=>'递四方检查单');
        foreach ($datalist as $key=>&$rs) {
            //维修类型
            $rs['repairType'] = $repairTypeAry[$rs['maintain_type_id']];
            if($rs['source_site_id']==9){
                $rs['repairType'] = '维保单,维修类型：'.$repairTypeAry[$rs['maintain_type_id']];
            }
            //获取产品
            $productList = $this->common_model->selectCustom('maintain_product',array('maintain_id'=>$rs['oid']));
            foreach ($productList as &$cp) {
                $priceData = $this->common_model->selectFirst('maintain_product_price',array('pid'=>$cp['product_id']));
                //gsh_output($priceData);
                $cp['priceData'] = $priceData;
                $rs['product_id'] = $cp['product_id'];
                $rs['imei'] = $cp['imei'];
                //获取品牌
                $rs['brand_id'] = $cp['brand_id'];
                $rs['product_model'] = $cp['product_model'];
                if($rs['brand_id']!=11)break;

                //获取配件
                $peijianList = $this->common_model->selectCustom('maintain_accessory',array('pid'=>$cp['product_id']));
                foreach ($peijianList as $pj) {
                    $rs['product_sku'] .= '  '. $pj['sku'];
                    $rs['product_sku_name'] .=  '  '.$pj['accessory_name'];
                    $rs['peijian_price'] .= '   '.$pj['accessory_price'];
                    $rs['peijian_cost'] .= '   '.$pj['accessory_cost'];
                }

                //获取配件价格
                $rs['price_total_sgf'] += $priceData['maintain_price'];
                $rs['price_total_yf'] += $priceData['freight_price'];
                $rs['price_total_pjf'] += $priceData['accessory_price'];
                $rs['cp_price_total'] += $priceData['maintain_price']+$priceData['freight_price']+$priceData['accessory_price'];
                $rs['price_sort'] = $priceData['price_sort'];
                $rs['product_quantity'] = max($cp['product_quantity'],1);

                //获取成本价格
                $sql = 'select * from maintain_accessory where pid = '.$cp['product_id'];
                $materialFirst = $this->common_model->getQuery($sql, 'row_array');
                $rs['cost_total'] += $materialFirst['accessory_cost'];

                switch ($cp['is_guarantee'])
                {
                    case 0:
                        $rs['guarantee_name'] = '保外';
                        break;
                    case 1:
                        $rs['guarantee_name'] = '电商保内';
                        break;
                    case 2:
                        $rs['guarantee_name'] = '品牌保内';
                        break;
                    default:
                        $rs['guarantee_name'] = '保外';
                }
                $productDataList[] = $rs;
            }
            unset($cp);
            //去掉没有报价的
            if(!$rs['price_total_sgf']){
                //unset($datalist[$key]);
            }
            if($rs['brand_id']!=11){
                unset($datalist[$key]);
            }
            $total_price += $rs['cp_price_total'];
        }
        unset($rs);

        $fieldData = array(
            'oid'=>'维修单号','product_id'=>'产品ID','product_model'=>'产品型号','source_int_id'=>'来源单号','repairType'=>'维修类型','consumer_email'=>'客户邮箱',
            'price_total_sgf'=>'手工费','price_total_yf'=>'运费','price_total_pjf'=>'配件费',
            'guarantee_name'=>'质保类型','price_sort'=>'货币种类','cp_price_total'=>'总额',
            'product_quantity'=>'产品数量','cost_total'=>'配件成本费','coupon_money'=>'优惠金额',
            'product_sku'=>'配件SKU','product_sku_name'=>'配件名称','peijian_price'=>'配件售价',
            'peijian_cost'=>'配件成本价','imei'=>'IMEI','append_money'=>'追加金额',
            'add_time'=>'添加时间','repairer'=>'维修员',
        );
        $title = $sitePrefix.'_'.substr($start_time,5).'~'.substr($end_time,5);
        $sheetTitle = '总额'.$total_price;
        gsh_output($productDataList,1);
        excel_output($title,$fieldData,$productDataList,$sheetTitle);
    }





    /**
     * @param string $sitePrefix
     * @param string $start_time
     * @param string $end_time
     * @param int $status_id
     */
    function daochu_cpsgf3($sitePrefix='es',$start_time = '',$end_time = '',$status_id=23,$iswlm=0){
        $start_time = empty($start_time)?'2016-01-01':$start_time;
        $end_time = empty($end_time)?date('Y-m-d'):$end_time;
        $siteFirst = $this->common_model->selectFirst('mt_repair_site',array('site_prefix'=>$sitePrefix));
        $siteId = $siteFirst['id'];
        //获取订单数据
        if($iswlm){
            $sql = "
        select p.*,t.*,o.* from maintain_product_price as p left join maintain_track as t on p.oid=t.maintain_id
left join maintain_order as o on p.oid=o.maintain_id
where t.target_status=$status_id and p.is_accept_price=1  and o.repair_site_id=$siteId and t.add_time>='$start_time' and t.add_time<'$end_time'
        ";
        }else{
            $sql = "
        select p.*,t.*,o.* from maintain_product_price as p left join maintain_track as t on p.oid=t.maintain_id
left join maintain_order as o on p.oid=o.maintain_id
where o.status_id=$status_id and o.repair_site_id=$siteId and t.add_time>='$start_time' and t.add_time<'$end_time'
        ";
        }

        $datalist = $this->common_model->getQuery($sql, 'result_array');
        $total_price = '';
        $productDataList = array();
        $repairTypeAry = array('6'=>'维修','7'=>'翻新','8'=>'退货','10'=>'返修','9'=>'递四方检查单');
        //website_origin
        $originAry = array(
            '1'=>'pandaClever','2'=>'valukect','3'=>'rakuten','4'=>'pandawill',
            '5'=>'elephone','6'=>'aliexpress','7'=>'carrefour','8'=>'zopo',
            '9'=>'morefine','10'=>'freefeet','11'=>'geekvida_es','12'=>'geekvida_pt'
        );
        foreach ($datalist as $key=>&$rs) {
            $rs['website_origin'] = $originAry[$rs['website_origin']];
            //维修类型
            $rs['repairType'] = $repairTypeAry[$rs['maintain_type_id']];
            if($rs['source_site_id']==9){
                $rs['repairType'] = '维保单,维修类型：'.$repairTypeAry[$rs['maintain_type_id']];
            }
            //获取产品
            $productList = $this->common_model->selectCustom('maintain_product',array('maintain_id'=>$rs['oid']));
            foreach ($productList as &$cp) {
                $priceData = $this->common_model->selectFirst('maintain_product_price',array('pid'=>$cp['product_id']));
                //gsh_output($priceData);
                $cp['priceData'] = $priceData;
                $rs['product_id'] = $cp['product_id'];
                $rs['imei'] = $cp['imei'];
                //获取品牌
                $rs['brand_id'] = $cp['brand_id'];
                $rs['product_model'] = $cp['product_model'];

                //获取配件
                $peijianList = $this->common_model->selectCustom('maintain_accessory',array('pid'=>$cp['product_id']));
                foreach ($peijianList as $pj) {
                    $rs['product_sku'] .= '  '. $pj['sku'];
                    $rs['product_sku_name'] .=  '  '.$pj['accessory_name'];
                    $rs['peijian_price'] .= '   '.$pj['accessory_price'];
                    $rs['peijian_cost'] .= '   '.$pj['accessory_cost'];
                }

                //获取配件价格
                $rs['price_total_sgf'] += $priceData['maintain_price'];
                $rs['price_total_yf'] += $priceData['freight_price'];
                $rs['price_total_pjf'] += $priceData['accessory_price'];
                $rs['cp_price_total'] += $priceData['maintain_price']+$priceData['freight_price']+$priceData['accessory_price'];
                $rs['price_sort'] = $priceData['price_sort'];
                $rs['product_quantity'] = max($cp['product_quantity'],1);

                //获取成本价格
                $sql = 'select * from maintain_accessory where pid = '.$cp['product_id'];
                $materialFirst = $this->common_model->getQuery($sql, 'row_array');
                $rs['cost_total'] += $materialFirst['accessory_cost'];

                switch ($cp['is_guarantee'])
                {
                    case 0:
                        $rs['guarantee_name'] = '保外';
                        break;
                    case 1:
                        $rs['guarantee_name'] = '电商保内';
                        break;
                    case 2:
                        $rs['guarantee_name'] = '品牌保内';
                        break;
                    default:
                        $rs['guarantee_name'] = '保外';
                }
                $productDataList[] = $rs;
            }
            unset($cp);

            //去掉没有报价的
            if(!$rs['price_total_sgf']){
                //unset($datalist[$key]);
            }
            $total_price += $rs['cp_price_total'];
        }
        unset($rs);

        $fieldData = array(
            'oid'=>'维修单号','website_origin'=>'购买来源站点','product_id'=>'产品ID','product_model'=>'产品型号','source_int_id'=>'来源单号','repairType'=>'维修类型','consumer_email'=>'客户邮箱',
            'price_total_sgf'=>'手工费','price_total_yf'=>'运费','price_total_pjf'=>'配件费',
            'guarantee_name'=>'质保类型','price_sort'=>'货币种类','cp_price_total'=>'总额',
            'product_quantity'=>'产品数量','cost_total'=>'配件成本费','coupon_money'=>'优惠金额',
            'product_sku'=>'配件SKU','product_sku_name'=>'配件名称','peijian_price'=>'配件售价',
            'peijian_cost'=>'配件成本价','imei'=>'IMEI','append_money'=>'追加金额',
            'add_time'=>'添加时间','repairer'=>'维修员',
        );
        $title = $sitePrefix.'_'.substr($start_time,5).'~'.substr($end_time,5);
        $sheetTitle = '总额'.$total_price;
        //gsh_output($productDataList,1);
        excel_output($title,$fieldData,$productDataList,$sheetTitle);
    }



    function daochu_bomsku(){
        $skuStr = 'sku49982_frc|sku49982_ca|sku49982_rcv|sku49982_upl|sku49983_upl|sku49983_fpa|sku49982_fpa|sku49983_fph|sku49982_fph|sku49982_mot|sku49982_spe|sku49982_wia|sku49982_bat|sku49982_sti|sku49982_ant|sku49982_cak|sku49982_fls|sku49982_lsh|sku49982_smp|sku49982_ruh|sku49982_ren|sku49982_por|sku49982_bln|sku49982_moe|sku49982_pad|sku49983_shh|sku49982_shh|sku49982_net|sku49982_Sew|sku49982_pcb|sku49983_lcd|sku49982_lcd|sku49982_unc|sku49982_had|sku49982_pam|sku62936_pcb|sku49953_bat|sku49953_ca|sku49953_frc|sku49954_upl|sku49953_upl|sku49953_mot|sku49953_spe|sku49953_gma|sku49953_wia|sku49953_Sew|sku49954_fph|sku49955_fph |sku49953_fph|sku49953_ruh|sku49953_fpa|sku49953_lsh|sku49953_rcv|sku49953_smp|sku49953_moe|sku49953_sti|sku49953_bln|sku49955_shh|sku49954_shh|sku49953_shh|sku49955_pcb|sku49953_pcb  |sku49954_lcd|sku49953_lcd|sku49953_unc|sku49953_had|sku49953_pam|sku49953_mis|sku56297_ca|sku56297_frc|sku56297_upl|sku59861_upl|sku56297_spe|sku56297_mot|sku56297_fpa|sku56297_ruh|sku56297_sew|sku56297_smp|sku56297_wia|sku56297_moe|sku56297_rcv|sku56297_lsh|sku56297_sti|sku56297_bat|sku56297_por|sku56297_ren|sku56297_pcb|sku56297_lcd|sku56297_unc|sku56297_had|sku56297_pam';
        $dataList = array();
        foreach (explode('|',$skuStr) as $val) {
            $bomFirst = $this->common_model->selectFirst('material_group',array('mg_sku'=>$val));
            $dataList[] = array(
                'sku'=>$bomFirst['mg_sku']?$bomFirst['mg_sku']:$val,
                'price'=>$bomFirst['mg_price']?$bomFirst['mg_price']:'未录入此SKU以及价格','model'=>$bomFirst['mg_type']);
        }
        $fieldData = array('sku'=>'sku','price'=>'价格','model'=>'型号');
        excel_output('skuBom',$fieldData,$dataList);
    }


    function daochu_cuige(){
        $dataList = $this->common_model->selectCustom('material_group','','mg_type desc');
        $fieldData = array(
            'mg_sku'=>'sku','mg_price'=>'价格','mg_type'=>'型号','mg_name'=>'名称'
        );
        excel_output('skuBom',$fieldData,$dataList);
    }


    /**
     * 导出手工费明细
     * @param string $sitePrefix
     * @param string $start_time
     * @param string $end_time
     */
    function daochu_cpsgf($sitePrefix='es',$start_time = '',$end_time = ''){
        //获取维修站点
        $repairSite = $this->common_model->selectFirst('mt_repair_site',array('site_prefix'=>$sitePrefix));
        $siteId = $repairSite['id'];
        if(!$siteId){
            show_error('repair site is not null');
        }
        //日期规则
        $start_time = empty($start_time)?'2015-12-01':$start_time;
        $end_time = empty($end_time)?date('Y-m-d'):$end_time;
        //获取订单数据
        $sql = 'select * from maintain_order where repair_site_id=\''.$siteId.'\' and status_id>=1
        and add_time > \''.$start_time.'\' AND add_time < \''.$end_time.'\' ';
        $datalist = $this->common_model->getQuery($sql, 'result_array');
        $total_price = '';
        $productDataList = array();
        foreach ($datalist as $key=>&$rs) {
            //获取产品
            $rs['productList'] = $this->common_model->selectCustom('maintain_product',array('maintain_id'=>$rs['maintain_id']));
            foreach ($rs['productList'] as &$cp) {
                if($cp['sort_id']!=1)continue;
                $priceData = $this->common_model->selectFirst('maintain_product_price',array('pid'=>$cp['product_id']));
                if(!$priceData['is_accept_price'])continue;
                //gsh_output($priceData);
                $cp['priceData'] = $priceData;
                //获取配件价格
                $rs['price_total_sgf'] += $priceData['maintain_price'];
                $rs['price_total_yf'] += $priceData['freight_price'];
                $rs['price_total_pjf'] += $priceData['accessory_price'];
                $rs['cp_price_total'] += $priceData['maintain_price']+$priceData['freight_price']+$priceData['accessory_price'];
                $rs['price_sort'] = $priceData['price_sort']?$priceData['price_sort']:$repairSite['price_sort'];
                $rs['product_quantity'] = max($cp['product_quantity'],1);
                switch ($cp['is_guarantee'])
                {
                    case 0:
                        $rs['guarantee_name'] = '保外';
                        break;
                    case 1:
                        $rs['guarantee_name'] = '电商保内';
                        break;
                    case 2:
                        $rs['guarantee_name'] = '品牌保内';
                        break;
                    default:
                        $rs['guarantee_name'] = '保外';
                }
                $productDataList[] = $rs;
            }
            unset($cp);
            //去掉没有报价的
            if(!$rs['price_total_sgf']){
                //unset($datalist[$key]);
            }
            $total_price += $rs['cp_price_total'];
        }
        unset($rs);
        $fieldData = array(
            'maintain_id'=>'维修单号','source_int_id'=>'来源单号','consumer_email'=>'客户邮箱',
            'price_total_sgf'=>'手工费','price_total_yf'=>'运费','price_total_pjf'=>'配件费',
            'guarantee_name'=>'质保类型','price_sort'=>'货币种类','cp_price_total'=>'总额',
            'product_quantity'=>'产品数量',
            'add_time'=>'添加时间','repairer'=>'维修员'
        );
        $title = $sitePrefix.'_'.substr($start_time,5).'~'.substr($end_time,5);
        $sheetTitle = '总额'.$total_price.$repairSite['price_sort'];
        excel_output($title,$fieldData,$productDataList,$sheetTitle);
    }




    /**
     * 导出统计手工费数据
     * @param string $start_time
     * @param string $end_time
     */
    function daochu_sgf($start_time = '',$end_time = '')
    {
        //日期规则
        $start_time = empty($start_time)?'2015-12-01':$start_time;
        $end_time = empty($end_time)?date('Y-m-d'):$end_time;

        //获取产品数据
        $sql = 'select * from maintain_product_price where add_time > \''.$start_time.'\' AND add_time < \''.$end_time.'\' ';
        $datalist = $this->common_model->getQuery($sql, 'result_array');

        //分组数据
        $exportData = array();
        foreach($datalist as $rs) {
            $productFirst = $this->common_model->selectFirst('maintain_product',array('product_id'=>$rs['pid']));
            if(!$productFirst){
                $this->common_model->delete('maintain_product_price',array('pid'=>$rs['pid']));
                continue;
            }
            $orderFirst = $this->common_model->selectFirst('maintain_order',array('maintain_id'=>$rs['oid']));
            if(!$orderFirst) {
                $this->common_model->delete('maintain_product_price',array('oid'=>$rs['oid']));
                continue;
            }
            $sitePrefix = substr($rs['oid'],0,2);
            switch ($sitePrefix) {
                case 'es':
                    $exportData['esData']['labor_price'] += $rs['maintain_price'];
                    break;
                case 'ru':
                    //gsh_output($rs['maintain_price'].'-----'.$rs['price_sort'].'---'.$rs['oid'].'----'.$rs['pid']);
                    if($rs['price_sort']=='USD'){
                        $exportData['ruData']['labor_price_usd'] += $rs['maintain_price'];
                    }else{
                        $exportData['ruData']['labor_price'] += $rs['maintain_price'];
                    }
                    break;
                case 'de':
                    $exportData['deData']['labor_price'] += $rs['maintain_price'];
                    break;
                case 'uk':
                    $exportData['ukData']['labor_price'] += $rs['maintain_price'];
                    break;
                case 'us':
                    $exportData['usData']['labor_price'] += $rs['maintain_price'];
                    break;
            }
        }

        //gsh_output($exportData,1);

        //导入EXCEL类
        $this->load->library('excel/PHPExcel');
        // 创建一个处理对象实例
        $objExcel = $this->phpexcel;
        //设置当前的sheet索引，用于后续的内容操作。
        $objExcel->setActiveSheetIndex(0);
        $objActSheet = $objExcel->getActiveSheet();
        //设置当前活动sheet的名称
        $objActSheet->setTitle('维修手工费统计');

        //设置当前值
        $objActSheet->setCellValue('A1',$start_time.'至'.$end_time.'之间全部数据共有'.count($datalist).'条维修数据');
        $es_str = '西班牙共产生手工费：'.$exportData['esData']['labor_price'].' EUR';
        $ru_str = '俄罗斯共产生手工费：'.$exportData['ruData']['labor_price'].' RUB ; 产生美元手工费（USD）:'.$exportData['ruData']['labor_price_usd'];
        $de_str = '德国共产生手工费：'.$exportData['deData']['labor_price'].' EUR';
        $uk_str = '英国共产生手工费：'.$exportData['ukData']['labor_price'].' GBP';
        $us_str = '美国共产生手工费：'.$exportData['esData']['labor_price'].' USD';
        $objActSheet->setCellValue('A2',$es_str);
        $objActSheet->setCellValue('A3',$ru_str);
        $objActSheet->setCellValue('A4',$de_str);
        $objActSheet->setCellValue('A5',$uk_str);
        $objActSheet->setCellValue('A6',$us_str);

        $outputFileName = 'erp_repair.'.date('m-d i:s').'.xls';
        $this->_outputfile($outputFileName,$objExcel);

    }



    /**
     * 导出统计数据
     * @param string $start_time
     * @param string $end_time
     */
    function daochu_oy($start_time = '',$end_time = '')
    {
        //日期规则
        $start_time = empty($start_time)?'2015-12-01':$start_time;
        $end_time = empty($end_time)?date('Y-m-d'):$end_time;

        //获取产品数据
        $sql = 'select p.sku,p.product_model,p.add_time,o.repairer,
        o.repair_site_id,o.status_id,o.maintain_id
        from  maintain_product as p
        LEFT JOIN maintain_order as o  on p.maintain_id=o.maintain_id
        where p.add_time > \''.$start_time.'\' AND p.add_time < \''.$end_time.'\' ';
        $datalist = $this->common_model->getQuery($sql, 'result_array');

        //分组数据
        $esData = $ruData = $deData = $ukData = $usData = array();
        foreach($datalist as $rs) {
            $status = intval($rs['status_id']);
            switch ($rs['repair_site_id']) {
                case 1:
                    $esData['allOne'][] = $rs;
                    if ($status >= 3) {
                        $esData['wxOne'][] = $rs;
                    } else {
                        $esData['wclOne'][] = $rs;
                    }
                    break;
                case 2:
                    $ruData['allOne'][] = $rs;
                    if ($status >= 3) {
                        $ruData['wxOne'][] = $rs;
                    } else {
                        $ruData['wclOne'][] = $rs;
                    }
                    break;
                case 3:
                    $deData['allOne'][] = $rs;
                    if ($status >= 3) {
                        $deData['wxOne'][] = $rs;
                    } else {
                        $deData['wclOne'][] = $rs;
                    }
                    break;
                case 4:
                    $ukData['allOne'][] = $rs;
                    if ($status >= 3) {
                        $ukData['wxOne'][] = $rs;
                    } else {
                        $ukData['wclOne'][] = $rs;
                    }
                    break;
                case 5:
                    $usData['allOne'][] = $rs;
                    if ($status >= 3) {
                        $usData['wxOne'][] = $rs;
                    } else {
                        $usData['wclOne'][] = $rs;
                    }
                    break;
            }
        }

        //导入EXCEL类
        $this->load->library('excel/PHPExcel');
        // 创建一个处理对象实例
        $objExcel = $this->phpexcel;
        //设置当前的sheet索引，用于后续的内容操作。
        $objExcel->setActiveSheetIndex(0);
        $objActSheet = $objExcel->getActiveSheet();
        //设置当前活动sheet的名称
        $objActSheet->setTitle('维修数据统计');

        //设置当前值
        $objActSheet->setCellValue('A1',$start_time.'至'.$end_time.'之间全部数据共有'.count($datalist).'条维修数据');
        $es_str = '西班牙共有：'.count($esData['allOne']).'条;维修中OR维修完成共有：'.count($esData['wxOne']).'条;未处理共有:'.count($esData['wclOne']);
        $ru_str = '俄罗斯共有：'.count($ruData['allOne']).'条;维修中OR维修完成共有：'.count($ruData['wxOne']).'条;未处理共有:'.count($ruData['wclOne']);
        $de_str = '德国共有：'.count($deData['allOne']).'条;维修中OR维修完成共有：'.count($deData['wxOne']).'条;未处理共有:'.count($deData['wclOne']);
        $uk_str = '英国共有：'.count($ukData['allOne']).'条;维修中OR维修完成共有：'.count($ukData['wxOne']).'条;未处理共有:'.count($ukData['wclOne']);
        $us_str = '美国共有：'.count($usData['allOne']).'条;维修中OR维修完成共有：'.count($usData['wxOne']).'条;未处理共有:'.count($usData['wclOne']);
        $objActSheet->setCellValue('A2',$es_str);
        $objActSheet->setCellValue('A3',$ru_str);
        $objActSheet->setCellValue('A4',$de_str);
        $objActSheet->setCellValue('A5',$uk_str);
        $objActSheet->setCellValue('A6',$us_str);

        $outputFileName = 'erp_repair.'.date('m-d i:s').'.xls';
        $this->_outputfile($outputFileName,$objExcel);

    }


    /**
     *  输出Excel文档公共方法
     * @param $outputFileName
     * @param $objExcel
     * @throws PHPExcel_Reader_Exception
     */
    public function _outputfile($outputFileName,$objExcel){
        //输出到文件
        //$objWriter->save($outputFileName);
        //将输出重定向到一个客户web浏览器
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$outputFileName.'"');
        header('Cache-Control: max-age=0');
        //如果你为IE 9,然后下面的可能需要
        header('Cache-Control: max-age=1');
        //如果你服务,即通过SSL,然后下面的可能需要
        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
        header ('Cache-Control: cache, must-revalidate');
        header ('Pragma: public');
        $objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }




    /**
     * 导入产品数据,无上传,直接访问文件
     * 行是从1开始，列是从0开始
     * getCellByColumnAndRow(col, row)
     * getCalculatedValue();可读取公式值
     */
    function in_laborPrice()
    {
        set_time_limit(0);

        //导入EXCEL类
        $this->load->library('excel/PHPExcel');
        $objReader = PHPExcel_IOFactory::createReader('Excel5'); //use excel2007 for 2007 format
        $objReader->setReadDataOnly(true);
        $upload_file = 'resource/uploadfiles/file/excels/partPriceList.xls';
        $objPHPExcel = $objReader->load($upload_file);
        $objWorksheet = $objPHPExcel->getActiveSheet();
        $highestRow = $objWorksheet->getHighestRow(); //总行数
        $highestColumn = $objWorksheet->getHighestColumn();
        $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); //总列数

        gsh_output('当前导入的excel文档路径:' . FCPATH . $upload_file);
        gsh_output('excel表格信息  总行数:' . $highestRow . ' &nbsp;&nbsp; 总列数:' . $highestColumnIndex);
        gsh_output('开始进入导入start............');


        /**
         * 表格维修项数据与数据库数据匹配
         */
        $partAry = array();
        $partMate = true;
        for ($i = 3; $i <= $highestRow; $i++) {
            $partVal = $objWorksheet->getCellByColumnAndRow(0, $i)->getValue();
            $where = array('maintain_part_id' => $partVal);
            $partFirst = $this->common_model->selectFirst('maintain_part', $where);
            if ($partFirst) {
                $partAry[] = array(
                    'part_id'=>$partFirst['maintain_part_id'],
                    'part_name'=>$partFirst['maintain_part_name'],
                    'part_grade'=>$partFirst['maintain_part_grade']
                );
            } else {
                $partMate = false;
                $partAry['未查到此数据' . $i] = $partVal;
            }
        }

        //gsh_output('读取维修项匹配的数据');
        //gsh_output($partAry,1);
        if (!$partMate) {
            gsh_output('表格的维修项与数据库的维修名称匹配失败，无法导入', 1);
        }

        /**
         * 维修站点
         */
        $siteAry = $this->common_model->selectCustom('mt_repair_site', array('is_enable' => 1));
        //gsh_output('读取站点数据');
        //gsh_output($siteAry,1);
        if (!$siteAry) {
            gsh_output('站点表还没有数据,导入失败',1);
        }

        /**
         * 输出excel表格价格数据
         */
        $excelData = array();
        for ($i = 3; $i <= $highestRow; $i++) {
            $partRows = array();
            for ($j = 4; $j < $highestColumnIndex; $j++) {
                $gridVal = $objWorksheet->getCellByColumnAndRow($j, $i)->getValue();
                $partRows[] = $gridVal?intval($gridVal):'';
                //$partRows[] = $gridVal;
            }
            $excelData[] = $partRows;
        }
        //gsh_output($excelData,1);
        if(!$excelData){
            gsh_output('excel表格没有读取到数据,导入失败',1);
        }

        /**
         * 合并excel价格数据与维修项数据
         */
        $newPartArr = array();
        foreach($excelData as $key=>$rsAry){
            //合并维修项到价格中
            $newPartArr[] = array_merge($rsAry,$partAry[$key]);
        }
        //gsh_output($newPartArr);

        /**
         * 开始导入
         */
        $siteArr = array(
            //ES
            1=>array(0,1),
            //RU
            2=>array(2,3),
            //DE
            3=>array(4,5),
            //UK
            4=>array(6,7),
        );
        $gradeRule = array(1=>12,2=>17,3=>30);
        foreach ($newPartArr as $key=>$rs) {
            $brand_base_price = $gradeRule[$rs['part_grade']];
            foreach ($siteArr as $k=>$v) {
                $inData = array(
                    'site_id'=>$k,
                    'labor_id'=>$rs['part_id'],
                    'individual_base_price'=>$rs[$v['0']],
                    'merchant_base_price'=>$rs[$v['1']],
                    'brand_base_price'=>$brand_base_price,
                );
                if($k==2){
                    $ruPrice = $rs[$v['0']];
                    $inData['ru_individual_base_price'] = $ruPrice;
                    $inData['individual_base_price'] = currency_service('RUB','USD',$ruPrice);
                }
                $this->common_model->insert('mt_labor_price',$inData);
            }
        }


        gsh_output('***************** 导入结束 *******************',1);
    }


    /**
     * 删除无用订单数据
     */
    function delData()
    {
        $sql = " select * from maintain_order where maintain_id in('es00005851','es00005852','es00005853','es00005854','es00005855','es00005856','es00005857','es00005858','es00005859','es00005860','es00005861','es00005862','es00005863','es00005864','es00005865','es00005866','es00005867','es00005868','es00005869','es00005870','es00005871','es00005872','es00005873','es00005874','es00005875','es00005876','es00005877','es00005878','es00005879','es00005880','es00005881','es00005882','es00005883','es00005884','es00005885','es00005886','es00005887','es00005888','es00005889','es00005890','es00005891','es00005892','es00005893','es00005894','es00005895','es00005896','es00005897','es00005898','es00005899','es00005900','es00005901','es00005902','es00005903','es00005904','es00005905','es00005906','es00005907','es00005908','es00005909','es00005910','es00005911','es00005912','es00005913','es00005914') ";
        $orderAll = $this->common_model->getQuery($sql, 'result_array');
        foreach ($orderAll as $key => $rs) {
            $productList = $this->product_model->get_product('', $rs['maintain_id']);
            foreach ($productList as $key => $cp) {
                $this->product_model->deleteData($cp['product_id']);
            }
            $where = array('maintain_id' => $rs['maintain_id']);
            //删除维修轨迹
            $this->common_model->delete('maintain_track', $where);
            //删除订单
            $this->common_model->delete('maintain_order', $where);
        }
    }


    /**
     * 维护订单
     */
    function setOrder(){
        $orderAll = $this->common_model->selectCustom('maintain_order');
        foreach ($orderAll as $key => $rs) {
            $typeid = trim($rs['maintain_type_id']);
            if(empty($typeid) || $typeid==1){
                /*
                $trackFirst = $this->common_model->selectFirst('',
                    array('maintain_id'=>$rs['maintain_id'],'target_status'=>'3')
                );
                */
                $this->common_model->update('maintain_order',
                    array('maintain_type_id'=>6),
                    array('maintain_id'=>$rs['maintain_id'])
                );
            }
        }
    }


    function showBrand(){
        $brandList = file_get_contents('http://list.jd.com/list.html?cat=9987,653,655&page=1&go=0&md=1&my=list_brand');
        $brandList = json_decode($brandList,true);
        $brands = $brandList['brands'];
        $time = time();
        foreach ($brands as $rs) {
            $this->common_model->insert('mt_brand',
                array('name'=>$rs['name'],'pinyin'=>$rs['pinyin'],'add_time'=>$time,'add_user'=>'system')
            );
        }

        gsh_output($brandList);
    }

    function  showWebsiteOrigin(){
        $origins = array('PANDACLEVER','VALUKNECT','RAKUTEN','PANDAWILL','ELEPHONE','ALIEXPRESS','CARREFOUR','ZOPO','MOREFINE','FREEFEET','GEEKVIDA_ES','GEEKVIDA_PT');
        $time = time();
        foreach ($origins as $val) {
            $this->common_model->insert('mt_website_origin',
                array('name'=>$val,'add_time'=>$time,'add_user'=>'system')
            );
        }

    }





}
